#include "stdio.h"
#include "stdlib.h"
typedef unsigned char *byte_pointer;
typedef unsigned int uint;

int x_combine_y(int x, int y)
{
    printf("x == %x\r\ny == %x\r\n",x,y);

    return (x & 0xff) | (y & 0xffffff00);
}
unsigned replace_byte(unsigned source, unsigned char replacement, int position)
{
    if((0 <= position) && (3 >= position))
    {
        source = source &~(0xff << (8 * position)) | (replacement << (8 * position));
    }
    return source;


}
int One_or_zero(int x)
{
    return !(x ^ -1) || !(x ^ 0) || !((x & 0xff000000) ^ 0xff000000) || !((x & 0xff) ^ 0);
}

int int_shifts_are_logical(void)
{
    return ((-1 >> 1) & 0x80000000) == 0x80000000;
}

int sra(int x, int k)
{
    int xsrl = (unsigned) x >> k;
    int w = 8*sizeof(int);   //32
    /*printf("w == %d\r\n",w);*/
    int MSBit = (x & (1 << (w - 1)));    //x & 0x80000000
    int OneOr0 = !((unsigned)MSBit ^ (1 << (w - 1)));
    /*printf("MSBit == %x\r\n OneOr0 == %x\r\n",MSBit,OneOr0);*/
    //if x < 0, bit[31:(32-k)] == 0b1111.....
    //if k == 1, bit[31] = -1 - (0x1 << (w - k -1) - 1)
    //  0xffffffff - 0x7fffffff
    /*printf("(0 - OneOr0) == %x\r\n((OneOr0 << (w - k )) - OneOr0) == %x\r\n(OneOr0 << (w - k ) == %x\r\n",(0 - OneOr0),((OneOr0 << (w - k )) - OneOr0),(OneOr0 << (w - k )));*/
    int Modified_value = ( 0 - OneOr0 - ((OneOr0 << (w - k )) - OneOr0));
    /*printf("xsrl == %x  Modified_value == %x\r\n",xsrl,Modified_value);*/
    xsrl |= Modified_value;
    return xsrl;
}

unsigned srl(unsigned x, int k)
{
    unsigned xsra = (int) x >> k;
    int w = 8*sizeof(int);
    /*printf("w == %d\r\n",w);*/
    int MSBit = (x & (1 << (w - 1)));    //x & 0x80000000

    int OneOr0 = !((unsigned)MSBit ^ (1 << (w - 1)));
    /*printf("MSBit == %x\r\n OneOr0 == %x\r\n",MSBit,OneOr0);*/
    //if MSBit == 0x80000000, there'll be a problem
    //  0xffffffff - 0x7fffffff
    /*printf("(0 - OneOr0) == %x\r\n((OneOr0 << (w - k )) - OneOr0) == %x\r\n(OneOr0 << (w - k ) == %x\r\n",(0 - OneOr0),((OneOr0 << (w - k )) - OneOr0),(OneOr0 << (w - k )));*/

    int Modified_value = ( 0 - OneOr0 - ((OneOr0 << (w - k )) - OneOr0));
    /*printf("xsra == %x  Modified_value == %x\r\n",xsra,Modified_value);*/
    xsra &= ~Modified_value;
    return xsra;

}

int even_ones(unsigned x)
{
    /*Return 1 when x contains an even number of 1s; 0 otherwise.*/
    x = x ^ (x >> 1);
    x = x ^ (x >> 2);
    x = x ^ (x >> 4);
    x = x ^ (x >> 8);
    x = x ^ (x >> 16);
    return x & 0x1;
}

int leftmost_one(unsigned x)
{
    /*Generate mask indicating leftmost 1 in x. Assume w = 32.*/
    /*For example 0xFF00 -> 0x8000, and 0x6600 -> 0x4000.*/
    /*If x = 0, then return 0.*/
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    return ((x + 1) >> 1);
}
int lower_bits( int x, int n)
{
    /*Mask with least signficant n bits set to 1*/
    /*Examples: n = 6 --> 0x3f, n = 17 --> 0x1FFFF*/
    /*Assume 1 <= n <= 2*/
    unsigned mask = ((((1 << (n - 1)) - 1) << 1) | 1);

    return x | mask;

}
void main(int argc, char *argv[])
{
    if(argc == 3)
    {
        printf("lower_bits(%x,%x) == %x \r\n",atoi(argv[1]),atoi(argv[2]),lower_bits(atoi(argv[1]),atoi(argv[2])));
    }
    else
        printf("Need only 1 argument!\r\n");
}
